package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.atguigu.tingshu.vo.user.UserListenProcessVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@Tag(name = "用户声音播放进度管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserListenProcessApiController {

    @Autowired
    private UserListenProcessService userListenProcessService;


    /**
     * 更新播放进度
     * /api/user/userListenProcess/updateListenProcess
     *
     * @param trackStatMqVo
     * @return
     */
    @PostMapping("/userListenProcess/updateListenProcess")
    @GuiGuLogin(required = false)
    public Result updateListenProcess(@RequestBody UserListenProcessVo userListenProcessVo) {
        Long userId = AuthContextHolder.getUserId();
        userListenProcessService.updateListenProcess(userListenProcessVo, userId);
        return Result.ok();
    }


    /**
     * 获取声音的上次跳出时间
     * /api/user/userListenProcess/getTrackBreakSecond/{trackId}
     *
     * @param trackId
     * @return
     */
    @GetMapping("/userListenProcess/getTrackBreakSecond/{trackId}")
    @GuiGuLogin(required = false)
    public Result<BigDecimal> getTrackBreakSecond(@PathVariable Long trackId) {
        // 此时需要判断是否为登录状态（不是登录状态时，不用返回上次跳出时间进度）
        Long userId = AuthContextHolder.getUserId();
        if (userId != null) {
            BigDecimal breakSecond = userListenProcessService.getTrackBreakSecond(trackId, userId);
            return Result.ok(breakSecond);
        }
        return Result.ok(new BigDecimal(0));
    }

    /**
     * 获取用户最近一次播放记录
     * /api/user/userListenProcess/getLatelyTrack
     *
     * @return
     */
    @GetMapping("/userListenProcess/getLatelyTrack")
    @GuiGuLogin
    public Result<Map<String, Long>> getLatelyTrack() {
        Long userId = AuthContextHolder.getUserId();

        Map<String, Long> latelyTrack = userListenProcessService.getLatelyTrack(userId);


        return Result.ok(latelyTrack);
    }
}

